package com.cy.tools;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class RedisPool {
    private static Properties properties;
    private static JedisPool jedisPool;
    private static RedisPool instance;
    //属性
    static{
        try{
        //加载properties属性文件
        properties=new Properties();
//        InputStream is = new FileInputStream(new File("jedis.properties"));
        InputStream is = RedisPool.class.getClassLoader().getResourceAsStream("jedis.properties");
        properties.load(is);
        String host=properties.getProperty("host");
        int port = Integer.parseInt(properties.getProperty("port"));
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.getProperty("maxTotal")));
            jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));

            jedisPool = new JedisPool(jedisPoolConfig,host,port);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

//    public static RedisPool getInstance(){
//        if ( instance == null) {
//            instance = new RedisPool();
//        }
//        return instance;
//    }

    //得到连接
    public static Jedis getRedio(){
        return jedisPool.getResource();
    }

//    public static void main(String[] args) {
//        System.out.println(new RedisPool().getRedio().ping());
//    }


}
